package com.geektime.hello.controller;

import com.alibaba.fastjson.JSON;
import com.geektime.hello.entity.User;
import com.geektime.hello.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author DRevelation
 * @Date 2020-06-30 下午 16:39
 * @Desc
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisUtils redisUtils;

    @GetMapping(value = "/set")
    public Boolean set(@RequestParam("key") String key, @RequestParam("value") String value) {
        return redisUtils.set(key, value);
    }

    @GetMapping(value = "/get")
    public Object get(@RequestParam("key") String key) {
        return redisUtils.get(key);
    }

    @GetMapping(value = "/setUser")
    public Boolean setUser() {
        User user = new User();
        user.setUsername("redis");
        user.setPassword("admin");
        user.setMobile("123456789");
        user.setCreateTime(LocalDateTime.now());
        user.setModifyTime(LocalDateTime.now());
        return redisUtils.set("user-" + user.getUsername(), JSON.toJSONString(user));
    }

    @PostMapping("/lhSet")
    public String lhSet(String key, String value) {
        redisUtils.lSet(key + "-list", value);
        redisUtils.hSet(key + "-hash", "a", value);
        redisUtils.hSet(key + "-hash", "b", value);
        return "======保存成功======";
    }
}
